package com.codingke.codingbookstore.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.codingke.codingbookstore.App;
import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.base.ViewHolder;
import com.codingke.codingbookstore.ui.ShoppingCartActivity;
import com.codingke.codingbookstore.utils.Constant;
import com.codingke.codingbookstore.utils.NumberUtils;
import com.codingke.codingbookstore.vo.Orders;

import java.util.ArrayList;

/**
 * descreption:
 * company: codingke.com
 * Created by vince on 15/11/25.
 */
public class ShoppingCartListItemEditAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<Orders> orderList;

    public ShoppingCartListItemEditAdapter(Context context, ArrayList<Orders> orderList) {
        this.context = context;
        this.orderList = orderList;
    }

    public void setOrderList(ArrayList<Orders> orderList) {
        this.orderList = orderList;
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.shopping_cart_list_item_edit, null);
        }
//         final Orders orders = orderList.get(position);
        ImageView imageView_book = ViewHolder.getView(convertView, R.id.imageView_book, null);
        TextView textView6_bookName = ViewHolder.getView(convertView, R.id.textView6_bookName, null);
        final TextView textView7_subtotal = ViewHolder.getView(convertView, R.id.textView7_subtotal, null);
        ImageView button_delete = ViewHolder.getView(convertView, R.id.button_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Orders o = orderList.get(position);
                o.delete(context);//删除服务器的购物车数据
                orderList.remove(position);//
                ((ShoppingCartActivity)context).setTotalAmountToUI();
                ((ShoppingCartActivity)context).setProductCount();
                ShoppingCartListItemEditAdapter.this.notifyDataSetChanged();
            }
        });

        Orders orders = orderList.get(position);
        textView6_bookName.setText(orders.getBookName());
        textView7_subtotal.setText("￥"+NumberUtils.format(orders.getSubtotal()));
        App.bitmapUtils.display(imageView_book, Constant.BMOBFILE_ROOT + orders.getBookImage());
        return convertView;
    }

//    @Override
//    public void setCalculationTotalAmount(TextView view) {
//        double totalAmount = 0;
//        Orders orders;
//        for (int i=0;i<orderList.size();i++){
//            orders = orderList.get(i);
//            totalAmount+=orders.getSubtotal();
//        }
//        view.setText("金额:"+NumberUtils.format(totalAmount)+"元");
//    }
}
